// scene.cpp
//

#include "scene.hpp"



// Model::Model
//
Model::Model ( std::string mesh_path )
  : mesh_path(mesh_path)
{
}



// Entity::Entity
//
Entity::Entity ( EntityFamilyID family,
                 Model &model )
  : family(family),
    model(model)
{
}



// SceneManager::register_entity_family
//
EntityFamilyID SceneManager::register_entity_family ( std::string name )
{
  EntityFamily def;
  def.id = registered_entity_families.size();
  def.name = name;
  registered_entity_families.push_back(def);
  return def.id;
}



// SceneManager::get_entity_family_id
//
EntityFamilyID SceneManager::get_entity_family_id ( const std::string &name )
{
  for (const auto &def: registered_entity_families)
    {
      if (def.name == name)
        return def.id;
    }
  GLT_FATAL("entity family not found: '%s'", name.c_str());
  return 0;
}



// Scene::Scene
//
Scene::Scene ( SceneManager &manager )
  : manager(manager)
{
}



// Scene::register_model
//
void Scene::register_model ( std::string name,
                             std::string obj_file )
{
  std::string path = datadir + "/models/" + obj_file;
  Model *model = new Model{path};
  models.emplace(name, model);

  GLT_TRACE("model registered: '%s' (%s)", name.c_str(), path.c_str());
}



// Scene::add_entity
//
Entity *Scene::add_entity ( EntityFamilyID family,
                            std::string model_name )
{
  GLT_ASSERT(models.contains(model_name));
  
  Model *model = models.at(model_name);
  Entity *entity = new Entity{family, *model};
  entities.push_back(entity);
  return entity;
}
